package yd.launcher.service;

import java.io.*;

public class Action implements Serializable {
    private String name;
    private String command;
    private boolean showCommandWindow;
    private int workingDirectoryOption;

    public Action(){}

    public Action(String name, String command, boolean showCommandWindow, int workingDirectoryOption) {
        this.name = name;
        this.command = command;
        this.showCommandWindow = showCommandWindow;
        this.workingDirectoryOption = workingDirectoryOption;
    }

    public String getName() {
        return name;
    }

    public String getCommand() {
        return command;
    }

    public boolean isShowCommandWindow() {
        return showCommandWindow;
    }

    public int getWorkingDirectoryOption() {
        return workingDirectoryOption;
    }

    //执行action
    public void exec() throws IOException {
        File workDirectory = null;
        if (workingDirectoryOption == 2) {
            workDirectory = new File(command.split(" ")[0]).getParentFile();
        } else if (workingDirectoryOption == 3) {
            String[] s = command.split(" ");
            workDirectory = new File(s[s.length-1]).getParentFile();
        }
        String executeCommand;
        if (showCommandWindow) {
            executeCommand = "cmd.exe /c start cmd.exe /k " + command;
        }else{
            executeCommand = "cmd.exe /c start " + command;
        }
        Process process = Runtime.getRuntime().exec(executeCommand, null, workDirectory);
        /*StringBuilder sb = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }*/
        /*try {
            int exitVal = process.waitFor();//如果需要等待子进程执行结束
            System.out.println(exitVal);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
    }
    //在新cmd中执行单个命令并读取执行结果
    public void execForResult(){
        try {
            Process process = Runtime.getRuntime().exec("cmd.exe /c" + command);

            // 读取命令的输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            int exitVal = process.waitFor();//如果需要等待子进程执行结束
            if (exitVal == 0) {
                System.out.println("Success!");
            } else {
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
    //执行cmd（需要指定工作目录）
    public void execWithProcessBuilder(){
        try {
            // 使用ProcessBuilder执行命令
            ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
            Process process = processBuilder.start();

            // 读取命令的输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            int exitVal = process.waitFor();
            if (exitVal == 0) {
                System.out.println("Success!");
            } else {
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
